﻿using System.Collections.Generic;
using JStore.Core.Commands;
using JStore.Core.Entities;
using JStore.Data.Daos;

namespace JStore.Tasks
{
    /// <summary>
    ///
    /// </summary>
    public class UserTypeTask
    {
        private readonly UserTypeDao _userTypeDao = new UserTypeDao();

        public List<UserType> GetAll()
        {
            return _userTypeDao.GetAll();
        }

        public UserType GetById(int id)
        {
            return _userTypeDao.GetById(id);
        }

        public bool Save(SaveUserTypeCmd cmd, out string errMsg)
        {
            errMsg = string.Empty;

            if (string.IsNullOrEmpty(cmd.UserTypeName))
            {
                errMsg = "类型名称为空";
                return false;
            }

            if (cmd.TjRate < 0 || cmd.TjRate >= 1)
            {
                errMsg = "推荐奖提成必须为0-1的小数";
                return false;
            }

            if (cmd.RepeatRate < 0 || cmd.RepeatRate >= 1)
            {
                errMsg = "复消扣比必须为0-1的小数";
                return false;
            }

            if (cmd.ShareDeductRate < 0 || cmd.ShareDeductRate >= 1)
            {
                errMsg = "分红扣比必须为0-1的小数";
                return false;
            }

            if (cmd.AreaTjRate < 0 || cmd.AreaTjRate >= 1)
            {
                errMsg = "区代推荐提成比必须为0-1的小数";
                return false;
            }

            if (cmd.AreaRepeatRate < 0 || cmd.AreaRepeatRate >= 1)
            {
                errMsg = "区代复消比必须为0-1的小数";
                return false;
            }

            if (cmd.AreaManageRate < 0 || cmd.AreaManageRate >= 1)
            {
                errMsg = "区代管理奖提成比必须为0-1的小数";
                return false;
            }

            if (cmd.AreaFosterRate < 0 || cmd.AreaFosterRate >= 1)
            {
                errMsg = "区代培育奖提成比比必须为0-1的小数";
                return false;
            }

            if (cmd.Id == null)
            {
                var info = new UserType
                               {
                                   UserTypeName = cmd.UserTypeName,
                                   AreaFosterRate = cmd.AreaFosterRate,
                                   AreaManageRate = cmd.AreaManageRate,
                                   AreaRepeatRate = cmd.AreaRepeatRate,
                                   AreaTjRate = cmd.AreaTjRate,
                                   RepeatRate = cmd.RepeatRate,
                                   ShareDeductRate = cmd.ShareDeductRate,
                                   TjRate = cmd.TjRate,
                                   JdCountPreLayer = cmd.JdCountPreLayer,
                                   AreaJdCountPreLayer = cmd.AreaJdCountPreLayer,
                               };
                _userTypeDao.Add(info);
                return true;
            }
            else
            {
                var info = _userTypeDao.GetById((int)cmd.Id);
                if (info == null)
                {
                    errMsg = "会员类型不存在";
                    return false;
                }

                info.UserTypeName = cmd.UserTypeName;
                info.AreaFosterRate = cmd.AreaFosterRate;
                info.AreaManageRate = cmd.AreaManageRate;
                info.AreaRepeatRate = cmd.AreaRepeatRate;
                info.AreaTjRate = cmd.AreaTjRate;
                info.RepeatRate = cmd.RepeatRate;
                info.ShareDeductRate = cmd.ShareDeductRate;
                info.TjRate = cmd.TjRate;
                info.JdCountPreLayer = cmd.JdCountPreLayer;
                info.AreaJdCountPreLayer = cmd.AreaJdCountPreLayer;
                _userTypeDao.Update(info);
                return true;
            }
        }

        public void DeleteById(int id)
        {
            _userTypeDao.DeleteById(id);
        }

        public List<UserType> GetByIds(List<int> ids)
        {
            return _userTypeDao.GetByIds(ids);
        }
    }
}

